import { eventBusGetter } from '../../utils/eventbus';
import { EVENT_BUS_KEY } from '../../common/constants';
import { getPrevInfo, getNextInfo } from '../../common/service/courses-service';

/**
 * 上一首
 */
export const playPrev = (id) => {
    let hasPrev = true;
    let obj = getPrevInfo(id);
    if (obj) {
        hasPrev = true;
        // 通知播放器播放新的音频
        eventBusGetter().$emit(EVENT_BUS_KEY.PLAY_LESSON_PREV, {
            lessonId: obj.lessonId
        });
    } else {
        hasPrev = false;
    }

    return hasPrev;
};

/**
 * 下一首
 */
export const playNext = (id) => {
    let hasNext = true;
    let obj = getNextInfo(id);
    if (obj) {
        hasNext = true;
        // 通知播放器播放新的音频
        eventBusGetter().$emit(EVENT_BUS_KEY.PLAY_LESSON_NEXT, {
            lessonId: obj.lessonId
        });
    } else {
        hasNext = false;
    }

    return hasNext;
};

export const playEnded = (id) => {
    return playNext(id);
    // let objAudio = e.detail.currentAudio || {};
    // playNext(objAudio.lessonId || 0);
};

/**
 * 获取当前播放列表中的按钮状态
 */
export const getPlayBtnStatus = (id) => {
    let objPrev = getPrevInfo(id);
    let objNext = getNextInfo(id);

    return {
        prevBtnStatus: !!objPrev,
        nextBtnStatus: !!objNext
    };
};
